package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.base.domain.Collect;
import cn.wolfcode.trip.base.domain.User;
import cn.wolfcode.trip.base.query.CollectQueryObject;
import cn.wolfcode.trip.base.service.ICollectService;
import cn.wolfcode.trip.base.util.JsonResult;
import cn.wolfcode.trip.base.util.UserContext;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/*
 * 收藏资源过滤器
 */
@RestController
@RequestMapping("/collects")
public class CollectController {

    @Autowired
    private ICollectService collectService;

    /**
     * 查询当前收藏的所有文章
     * @param qo
     * @return
     */
    @GetMapping("/userlikes")
    public PageInfo queryForList(CollectQueryObject qo){
        PageInfo<Collect> pageInfo = collectService.queryForList(qo);
        return pageInfo;

    }

    /**
     * 查询当前文章是否被收藏
     * @param qo
     * @return
     */
    @GetMapping("/{type}/{collectId}")
    public Boolean queryfortravel(CollectQueryObject qo){
        User user = UserContext.getUser();
        qo.setUserId(user.getId());
        return collectService.queryfortravel(qo);
    }

    /**
     * 查询被收藏数量
     * @param qo
     * @return
     */
    @GetMapping("/count/{type}/{collectId}")
    public int queryforCount(CollectQueryObject qo){
        return collectService.queryforCount(qo);
    }
    /**
     * 删除当前游记被收藏
     * @param qo
     * @return
     */
    @DeleteMapping("/{type}/{collectId}")
    public JsonResult delete(CollectQueryObject qo){
        User user = UserContext.getUser();
        qo.setUserId(user.getId());
        collectService.delete(qo);
        return new JsonResult();


    }
    /**
     * 添加收藏当前游记
     * @param qo
     * @return
     */
    @PostMapping("/{type}/{collectId}")
    public JsonResult save(CollectQueryObject qo){
        collectService.save(qo);
        return new JsonResult();

    }

}
